//
//  EditNameViewController.m
//  B2C
//
//  Created by 李小龙 on 2017/7/27.
//  Copyright © 2017年 lixiaolong. All rights reserved.
//

#import "EditNameViewController.h"

@interface EditNameViewController ()

@end

@implementation EditNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self createNavBar];
}

- (void)createNavBar {
    switch (self.type) {
        case EditTypeName:
            self.title = @"修改姓名";
            [self.textField setPlaceholder:@"请输入姓名"];
            if (TextIsNotNil([UserInfo shareInstance].user.mname)) {
                [self.textField setText:[UserInfo shareInstance].user.mname];
            }
            break;
        case EditTypeNickName:
            self.title = @"修改昵称";
            [self.textField setPlaceholder:@"请输入昵称"];
            if (TextIsNotNil([UserInfo shareInstance].user.mnickname)) {
                [self.textField setText:[UserInfo shareInstance].user.mnickname];
            }
        default:
            break;
    }
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(rightBarButtonItemAction:)];
    NSDictionary *dic = [NSDictionary dictionaryWithObject:[UIColor colorWithHex:0x030303 alpha:1.0] forKey:NSForegroundColorAttributeName];
    [right setTitleTextAttributes:dic forState:UIControlStateNormal];
    self.navigationItem.rightBarButtonItem = right;
}

- (void)rightBarButtonItemAction:(id)sender {
    NSString *alertStr, *uploadUrl, *keyStr;
    
    switch (self.type) {
        case EditTypeName:
            alertStr = @"请输入姓名";
            uploadUrl = URL_Save_MName;
            keyStr = @"mname";
            break;
        case EditTypeNickName:
            alertStr = @"请输入昵称";
            uploadUrl = URL_Save_MNickname;
            keyStr = @"mnickname";
            break;
        default:
            break;
    }
    if (!TextIsNotNil(self.textField.text)) {
        [self showAlertMessage:alertStr];
        return;
    }
    
    [MBProgressHUD showHUDProgress:self.view animated:YES];
    [MYAFNetworking POST:uploadUrl parameters:@{keyStr:self.textField.text} success:^(NSURLSessionDataTask * _Nullable task, id  _Nullable responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if ([responseObject[@"code"] isEqualToString:@"200"]) {
            switch (self.type) {
                case EditTypeName:
                    [UserInfo shareInstance].user.mname = self.textField.text;
                    break;
                case EditTypeNickName:
                    [UserInfo shareInstance].user.mnickname = self.textField.text;
                    break;
                default:
                    break;
            }
            [MBProgressHUD myShowHUDAddedTo:self.view labelText:@"保存成功" animated:YES];
            [self disMissSelf];
        } else {
            [MBProgressHUD showError:self.view title:responseObject[@"message"] code:responseObject[@"code"] animated:YES];
        }
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nullable error) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        [MBProgressHUD showError:self.view title:@"" code:@"" animated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
